//附件 chaoxian.txt是个文本文件，用程序把其中的金正恩替换成 金三胖，
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class test {
    public static void main(String[] args) {
        // 文件路径
        String filePath = "C:\\Users\\唐凤穎\\Desktop\\chaoxian.txt";
        // 读取文件内容并替换字符串
        String content = readFileContent(filePath);
        // 替换"金正恩"为"金三胖"
        String replacedContent = content.replace("金正恩", "金三胖");
        // 将替换后的内容写回文件
        writeFileContent(filePath, replacedContent);
    }

    private static String readFileContent(String filePath) {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String currentLine;
            while ((currentLine = reader.readLine()) != null) {
                contentBuilder.append(currentLine).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentBuilder.toString();
    }

    private static void writeFileContent(String filePath, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}